/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import spagcore.SpaglCoreList;

/**
 * A List (org.eclipse.swt.widgets.List) that conforms to the SpaglCoreList interface
 * @author Sam Pottinger
 */
public class SpaglList implements SpaglSwtWidget, SpaglCoreList {
	
	private List innerWidget;
	private SpaglCoreContainer parentWidget;
	private SpaglCoreFont widgetFont;
	public static int SINGLE = SWT.SINGLE;
	public static int MULTI = SWT.MULTI;
	
	/**
	 * Creates a list with the default layout behavior
	 * @param newParent The container that will hold this widget
	 * @param newSelectionMode The selection mode to be used by this list (either the int values of the class variables SINGLE or MULTI)
	 */
	public SpaglList(SpaglCoreContainer newParent, int newSelectionMode)
	{
		innerWidget = new List(((SpaglSwtContainer)newParent).getComposite(), SWT.BORDER | newSelectionMode | SWT.H_SCROLL | SWT.V_SCROLL);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a list with specified parameters for the layout manager
	 * @param newParent The container that will hold this widget
	 * @param newSelectionMode The selection mode to be used by this list (either the int values of the class variables SINGLE or MULTI)
	 * @param parameters The string parameters to be used for the layout manager
	 */
	public SpaglList(SpaglCoreContainer newParent, int newSelectionMode, String parameters)
	{
		innerWidget = new List(((SpaglSwtContainer)newParent).getComposite(), SWT.BORDER | newSelectionMode | SWT.V_SCROLL);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}

	/**
	 * Gets the native SWT List (org.eclipse.swt.widgets.List) widget for this object
	 * @return An SWT List
	 */
	public List getNativeWidget() {
		return innerWidget;
	}

	/**
	 * Gets the parent container that is holding this widget
	 * @return This object's parent container as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the height in pixels of this widget
	 * 
	 * @return The vertical size of this widget
	 */
	public int getHeight() {
		return innerWidget.getBounds().y;
	}

	/**
	 * Gets the width in pixels of this widget
	 * 
	 * @return The horizontal size of this widget
	 */
	public int getWidth() {
		return innerWidget.getBounds().x;
	}

	/**
	 * Sets the size of the object in pixels
	 * 
	 * @param width the horizontal size in size x
	 * @param height the vertical size in size y
	 */
	public void setSize(int width, int height) {
		innerWidget.setSize(width, height);
	}

	/**
	 * Adds an object to this list as an option
	 * @param newElement The new object to be added
	 */
	public void addElement(Object newElement) {
		innerWidget.add(newElement.toString());
	}

	/**
	 * Adds an object to this list as an option at a given position
	 * @param position The integer position where the object will be inserted
	 * @param newElement The new object to be added
	 */
	public void addElement(int position, Object newElement) {
		innerWidget.add(newElement.toString(), position);
	}

	/**
	 * Resets the selected index to none
	 */
	public void clearSelection() {
		innerWidget.deselectAll();
	}

	/**
	 * Gets the most recently selected option or -1 if no option is currently selected
	 * @return the integer index of the most recenetly selected object
	 */
	public int getSelectedIndex() {
		return innerWidget.getSelectionIndex();
	}

	/**
	 * Gets the indicies currently selected in this list
	 * @return An array of indicies currently highlighted in this widget
	 */
	public int[] getSelectedIndicies() {
		return innerWidget.getSelectionIndices();
	}

	/**
	 * Completely clears all selection options from this list
	 */
	public void removeAllElements() {
		innerWidget.removeAll();
	}

	/**
	 * Removes the object at a given index from this list
	 * @param i The index of the option to be removed
	 */
	public void removeElement(int i) {
		innerWidget.remove(i);
	}

	/**
	 * Sets the list of options available
	 * @param newElementList A list of objects to be used as selection options
	 */
	public void setElementList(Object[] newElementList) {
		String[] newStringList = new String[newElementList.length];
		for(int i = 0; i<newElementList.length; i++)
			newStringList[i] = newElementList[i].toString();
		innerWidget.setItems(newStringList);
	}

	/**
	 * Sets the option highlighted in this widget to the option at the specified index
	 * @param i The index of the option to be selected
	 */
	public void setSelectedIndex(int i) {
		innerWidget.setSelection(i);
	}

	/**
	 * Sets the options to be highlighted as selected in this list
	 * @param i The indicies of the objects to be highlighted as selected
	 */
	public void setSelectedIndicies(int[] i) {
		innerWidget.setSelection(i);
	}

	/**
	 * Gets the font currently used by this widget
	 * @return The SpaglFont used by this widget as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
	}
	
	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
		if(dispose)
		{
			widgetFont = null;
			((SpaglFont)newFont).dispose();
		}
		else
			widgetFont = newFont;
	}
	
	/**
	 * Disposes this widget through SWT
	 */
	public void dispose()
	{
		innerWidget.dispose();
	}
	
	/**
	 * Adds an event listener to this button
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object targetObject){
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {innerWidget.addListener(SWT.Selection, new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.SELECTION_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}

}
